package cn.ciis.ciis.region;

import cn.ciis.basic.base.BaseController;
import cn.ciis.basic.model.Region;
import cn.ciis.ciis.enterprise.EnterpriseAdminService;
import cn.ciis.ciis.equipment.EquipmentAdminService;
import cn.ciis.ciis.regionequiment.RegionEquimentAdminService;
import cn.ciis.sys.permission.Notes;
import cn.ciis.sys.permission.Remark;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

/**
 * 区域信息管理控制器
 * 描述：
 * @author 冉志林
 */
public class RegionAdminController extends BaseController {

    @Inject RegionAdminService srv;
    @Inject
    EnterpriseAdminService enterpriseAdminService;
    @Inject
    EquipmentAdminService equipmentAdminService;
    @Inject
    RegionEquimentAdminService regionEquimentAdminService;

	@Notes(value = "区域信息管理")
    @Remark(value = "区域信息列表")
    public void index() {
        Region mode = getModel(Region.class, "", true);
        kModel(mode, srv.paginate(getInt(PAGENUMBER, 1), getInt(PAGESIZE, 10), mode));
    }

    @Remark(value = "区域信息添加或编辑页")
    public void addOrEdit(Integer id) {
        set("enterpriseList",enterpriseAdminService.findAll());
	    kAddOrEdit(srv.findById(id));
    }

    @Remark(value = "区域信息保存或修改")
    @Before(RegionAdminValidator.class)
    public void saveOrUpdate() {
        Region bean = getBean(Region.class);
        renderJson(srv.saveOrUpdate(bean));
    }

    @Remark(value = "区域信息删除")
    public void deletes(String ids) {
        renderJson(srv.deletes(ids));
    }


    @Remark(value = "区域配置设备信息页面")
    public void config(String id){
        if (StrKit.notBlank(id)) {
            set("rId",id);
            set("equipmentList",equipmentAdminService.findEquipmentListByEnterprise(id));//未有配置的设备信息
            set("checkedEquimentList",regionEquimentAdminService.findCheckedRegionEquiment(id));//当前区域已经配置的设备
        }
    }

    @Remark(value = "区域配置设备信息")
    public void zoneConfigurationDevice(int rid,int eid,int type){
        if (0==type){
            Record byRegionEquiment = regionEquimentAdminService.findByRegionEquiment(rid, eid);
            if (byRegionEquiment!=null) {
                kFail("系统已经存在当前信息,请联系管理员查看");
                return;
            }
            Record record=new Record().set("rId",rid).set("eId",eid);
            kRenderJson(Db.save("ciis_region_equipment", record));
        }else if(1==type){
            kRenderJson(regionEquimentAdminService.deleteByRegionEquiment(rid,eid));
        }
    }

}